namespace Unosquare.Labs.Scratchpad
{
    using System;
    using Microsoft.SPOT;
    using Unosquare.Labs.Drivers.Vizic;
    using Unosquare.Labs.Utils;

    /// <summary>
    /// This sample shows how touch eventing works.
    /// Remember that touch eventing spawns a new thread that could
    /// potentially be too cycle-intensive for smaller
    /// processors, so be mindful about this when enabling touch eventing
    /// </summary>
    public static class SampleTouchEvents
    {
        static public void RunSample(VizicBaudRates baudRate)
        {
            var lcd = new VizicSmartGpu();
            lcd.BaudRate = baudRate;

            // wire up the events
            lcd.TouchData += lcd_TouchData;
            lcd.TouchPositionDrag += lcd_TouchPositionDrag;
            
            // Enable the touch eventing thread
            lcd.TouchEventingEnabled = true;
        }

        #region Test Methods

        private static byte[] GenerateImage(int imageWidth)
        {

            var image = new byte[imageWidth * imageWidth * 3];
            var color = 56;


            // Create the sample pixels
            for (var b = 0; b < image.Length; b++)
            {
                image[b] = (byte)color;
                color++;
                if (color >= 256)
                    color = 56;
            }

            return image;
        }

        private static void SaveImageToDisk(VizicSmartGpu lcd)
        {
            lcd.FileOpen("img.bmp", VizicFileModes.OpenOrTruncate);
            lcd.FileClose();
            lcd.FileOpen("img.bmp", VizicFileModes.WriteOnly);
            lcd.FileWrite(GenerateImage(20));
            lcd.FileFlush();
            lcd.FileClose();
        }

        private static void DrawImageFromBytes(VizicSmartGpu lcd)
        {

            ushort imageWidth = 125;
            var image = GenerateImage(imageWidth);
            ushort offset = 60;
            lcd.WriteString(40, 40, 300, 200, VizicFontSizes.Size10, "Drawing Image . . .");
            var startTime = DateTime.Now;
            lcd.DrawImage(offset, offset, imageWidth, imageWidth, image);
            var elapsedSeconds = DateTime.Now.Subtract(startTime).TotalMilliseconds() / 1000;
            lcd.WriteString(150, 40, 300, 200, VizicFontSizes.Size10, "Drawn in " + elapsedSeconds + "s");
        }

        #endregion

        #region Touch Event Handlers

        static void lcd_TouchPositionDrag(object sender, VizicDragEventArgs e)
        {
            var lcd = sender as VizicSmartGpu;
            if (lcd.SleepMode == false)
                lcd.DrawLine(e.Start.X, e.Start.Y, e.End.X, e.End.Y);
        }

        static void lcd_TouchData(object sender, VizicTouchEventArgs e)
        {
            if (e.Data.ResponseType == VizicTouchResponseTypes.PositionData)
                return;

            var lcd = sender as VizicSmartGpu;
            if (lcd.SleepMode) lcd.SleepMode = false;

            if (e.Data.ResponseType == VizicTouchResponseTypes.HomeIcon)
                lcd.ClearScreen();

            if (e.Data.ResponseType == VizicTouchResponseTypes.SongIcon)
                lcd.SDShowImage(0, 0, "bug24");

            if (e.Data.ResponseType == VizicTouchResponseTypes.TelephoneIcon)
                DrawImageFromBytes(lcd);

            if (e.Data.ResponseType == VizicTouchResponseTypes.MessageIcon)
            {
                lcd.SleepMode = true;
            }

            if (e.Data.ResponseType == VizicTouchResponseTypes.BookIcon)
            {
                lcd.SleepMode = false;
            }
        }

        #endregion
    }
}
